package com.leetcode.doublePointer;

import java.util.*;

/**
 * 双指针 leetcode345 反转字符串中的元音字母
 */
public class Solution345 {
    private static Set<Character> vowel = new HashSet<>(Arrays.asList('a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'));

    public String reverseString(String s) {
        if (s == null || s.length() == 0) {
            return s;
        }

        char[] result = s.toCharArray();

        // 定义双指针，一头一尾
        int start = 0;
        int end = s.length() - 1;
        while (start < end) {
            while (start < end && !isVowelChar(result[start])) {
                start++;
            }

            while (end > start && !isVowelChar(result[end])) {
                end--;
            }

            swap(result, start, end);
            start++;
            end--;
        }

        return String.valueOf(result);
    }

    // 判断字符是否是元音字母
    private static boolean isVowelChar(Character c) {
        if (vowel.contains(c)) {
            return true;
        }

        return false;
    }

    // 交换两个字符位置
    private static void swap(char[] arr, int posA, int posB) {
        char tmp = arr[posA];
        arr[posA] = arr[posB];
        arr[posB] = tmp;
    }

    public static void main(String[] args) {
        Solution345 solution345 = new Solution345();
        Scanner scanner = new Scanner(System.in);
        System.out.println(solution345.reverseString(scanner.next()));
    }
}
